/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Filter;
import com.googlecode.genericdao.search.Search;
import org.apache.commons.lang3.StringUtils;
import org.crystalrange.searching.SearchOperatorListitemRenderer;
import org.crystalrange.searching.SearchOperators;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Krzysiek
 */
public class DocumentReferenceTypeSearchController extends GenericForwardComposer {
    
    protected Listbox sortOperatorName;
    protected Listbox sortOperatorDescription;
    protected Search searchDocumentReferenceType;
    protected Textbox textboxName;
    protected Textbox textboxDescription;

    public Search getSearchDocumentReferenceType() {
        return searchDocumentReferenceType;
    }

    public void setSearchDocumentReferenceType(Search searchDocumentReferenceType) {
        this.searchDocumentReferenceType = searchDocumentReferenceType;
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        
        this.sortOperatorName.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorName.setItemRenderer(new SearchOperatorListitemRenderer());

        this.sortOperatorDescription.setModel(new ListModelList(SearchOperators.getAllOperators()));
        this.sortOperatorDescription.setItemRenderer(new SearchOperatorListitemRenderer());
        
        this.searchDocumentReferenceType = (Search) this.execution.getAttribute("searchDocumentReferenceType");
        
        if (this.searchDocumentReferenceType != null) {
            for (Filter filter : this.searchDocumentReferenceType.getFilters()) {
                if (filter.getProperty().equals("name")) {
                    this.restoreOperator(this.sortOperatorName, filter);
                    this.textboxName.setValue(filter.getValue().toString());
                } else if (filter.getProperty().equals("description")) {
                    this.restoreOperator(this.sortOperatorDescription, filter);
                    this.textboxDescription.setValue(filter.getValue().toString());
                }
            }
        }
        
        component.setAttribute("controller", this);
    }

    private void restoreOperator(Listbox listbox, Filter filter) {
        if (filter.getOperator() == Filter.OP_EQUAL) {
            listbox.setSelectedIndex(1);
        } else if (filter.getOperator() == Filter.OP_NOT_EQUAL) {
            listbox.setSelectedIndex(2);
        } else if (filter.getOperator() == Filter.OP_LESS_THAN) {
            listbox.setSelectedIndex(3);
        } else if (filter.getOperator() == Filter.OP_GREATER_THAN) {
            listbox.setSelectedIndex(4);
        } else if (filter.getOperator() == Filter.OP_LESS_OR_EQUAL) {
            listbox.setSelectedIndex(5);
        } else if (filter.getOperator() == Filter.OP_GREATER_OR_EQUAL) {
            listbox.setSelectedIndex(6);
        } else if (filter.getOperator() == Filter.OP_ILIKE) {
            listbox.setSelectedIndex(7);
        }
    }
    
    public void onClick$buttonOk(Event event) throws Exception {
        this.searchDocumentReferenceType.getFilters().clear();

        if (StringUtils.isNotEmpty(this.textboxName.getText())) {
            Listitem item = this.sortOperatorName.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchDocumentReferenceType.addFilter(new Filter("name", this.textboxName.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        if (StringUtils.isNotEmpty(this.textboxDescription.getText())) {
            Listitem item = this.sortOperatorDescription.getSelectedItem();
            if (item != null) {
                SearchOperators searchOperator = (SearchOperators) item.getValue();
                if (searchOperator.getSearchOperatorId() != -1) {
                    this.searchDocumentReferenceType.addFilter(new Filter("description", this.textboxDescription.getValue(), searchOperator.getSearchOperatorId()));
                }
            }
        }

        this.execution.setAttribute("searchDocumentReferenceType", this.searchDocumentReferenceType);
        this.execution.setAttribute("result", "ok");
        this.self.detach();
    }
    
    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("searchDocumentReferenceType", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }
}
